<!DOCTYPE html>
<html lang="en" class="page-fill" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>用户列表--快递物流系统</title>
	<link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all" />
</head>
<body>
	<blockquote class="layui-elem-quote quoteBox">
		<form class="layui-form" lay-filter="searchForm">>
			<div class="layui-inline">
				<div class="layui-inline">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" name="name"
							placeholder="请输入姓名" />
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">客户编号</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" name="clientId"
							placeholder="请输入员工编号" />
					</div>
				</div>
				
 			<button type="submit" class="layui-btn search_btn" lay-submit="" lay-filter="search">搜索</button>

 				</div>
			<div class="layui-inline">
				<a class="layui-btn layui-btn-normal addcustomer_btn">添加员工</a>
				<a class="layui-btn layui-btn outcustomer_btn">导出客户</a>
				<a class="layui-btn layui-btn-normal addcustomer_btn">财务</a>
			</div>

		</form>
	</blockquote>
	<table id="customerList" lay-filter="customerList"></table>

	<!--操作-->
	<script type="text/html" id="customerListBar">
		<a class="layui-btn layui-btn-xs " lay-event="edit">编辑</a>
		<button class="layui-btn layui-btn-xs layui-btn-danger delete_btn" lay-event="del">删除</button>
		<button class="layui-btn layui-btn-xs  add_btn" lay-event="address">地址列表</button>
	</script>


	<script type="text/javascript"  th:src="@{/lib/layui/layui.js}"></script>
	<script type="text/javascript" th:inline="none">

layui.use(['table','layer','jquery','form'], function(){
  table = layui.table;
  layer = parent.layer === undefined ? layui.layer : top.layer;
  $ = layui.jquery;
  form = layui.form;
  var rank = 1;

  
  //员工表的数据渲染
 tableIns = table.render({
    elem: '#customerList'
    ,height: 300
    ,id:  'customerListTable'
    ,url: '/api/customer/list?rank=1' //rank=1 代表高级客户
    ,page: true //开启分页
    ,cols: [[ //表头
    	{
			title: "全选",
			width: 80,
			type: "checkbox",
			fixed: 'left'}
      ,{field: 'clientId', title: '序号', width:150, sort: true, fixed: 'left'}
      ,{field: 'loginAccount', title: '用户编号', width:150}
      ,{field: 'clientName', title: '姓名', width:120}
      ,{field: 'cellphone', title: '手机', width: 130}
      ,{field: 'telephone', title: '座机', width: 130}
      ,{field: 'tax', title: '传真', width: 130}
      ,{field: 'mail', title: '邮箱', width: 130}
      ,{field: 'clientType', title: '用户等级', width: 130}
      ,{
			title : "操作",
			fixed : 'right',
			width : 200,
			align : 'center',
			toolbar : '#customerListBar'
		}
     
    ]]
  });
  

  
  
  	//添加员工事件
    		$(".addcustomer_btn").click(function() {
    			addcustomer();
    		});
  
  
  function addcustomer(edit){
  	var op = "";
  	var id = "";
	  if (edit == undefined || edit == null) {
		  op = "新增";
		  params = "addCustomer";

	  } else {
		  op = "编辑";
		  params = "updateCustomer";
		  id = "/"+edit.clientId;
	  }
      var index = layui.layer.open({
          title : op+"客户",
          type : 2,
		  area: ['780px','690px'],
          content : "/page/"+params+id,

      })

  }
  
  
  //编辑高级客户的收件地址的信息
  function addressEdit(edit){
  	var id = edit.clientId;
  	var index = parent.layer.open({
			title : '选择目的地',
			type : 2,
			area : [ '1000px', '580px' ], //宽高
			content : '/page/addressList/'+id,//获取该客户的所有地址信息
		});

  }
  
  
  
 //根据客户号，姓名，查询客户信息
   form.on('submit(search)', function(data){

 	var params = JSON.stringify(data.field);
  	 tableIns.reload({

 		    where : data.field
 		  
 		});
 	return false;  
 });   
  
 //监听行工具栏事件
	  table.on('tool(customerList)', function(obj){//能获取一行的data?
	        var layEvent = obj.event,
	            data = obj.data;

	        if(layEvent === 'edit'){ //编辑
	        	addcustomer(data);
	            
	        } else if(layEvent === 'address'){
	        	addressEdit(data)
	        } else if(layEvent === 'del'){ //删除
	            layer.confirm('确定删除该用户信息？',{icon:3, title:'提示信息'},function(index){
	            	
	                $.get("/api/customer/delete",{
	                    clientId : data.clientId, //将需要删除的员工编号作为参数传入
	                    clientType: rank

	                },function(data){
	                	if (data.code == 200) {
	                		tableIns.reload(); //刷新表格数据（也即让table重新加载url）
		                    layer.close(index);//关闭弹出框
	                	} else {
	                		 layer.close(index);//关闭弹出框
	                		 layer.msg(data.msg,{icon:2});
	                	}
	                    
	                })
	            });
	        } else if(layEvent === 'look'){ //预览
	            layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
	        }
	    });
 
 
   //批量删除
   $(".delete_btn").click(function(){
       var checkStatus = table.checkStatus('customerListTable'),
           data = checkStatus.data,
           ids = [];
       if(data.length > 0) {
           for (var i in data) {
               ids.push(data[i].clientId);
           }
           ids = ids.join(",");
          var rank = 1;
           layer.confirm('确定删除选中的员工？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("/api/customer/delete",{
                    client_ids : ids,  //将需要删除的Ids作为参数传入
                    rank: rank
                },function(d){
                	if (d.code == 200) {
						layer.msg("删除成功", {
							"icon" : 1
						});
						tableIns.reload();
					} else {
						layer.msg(d.msg, {
							"icon" : 2
						});
					}
                })
           })
       }else{
           layer.msg("请选择需要删除的用户");
       }
   })
  
  
});
</script>

</body>
</html>